package com.xiaomiMall.mange.service.Impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.xiaomiMall.mange.domain.MallRegisterBody;
import com.xiaomiMall.mange.domain.MallUser;
import com.xiaomiMall.mange.service.MallRegisterService;
import com.xiaomiMall.mange.service.MallUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

@Service
public class MallRegisterServiceImpl implements MallRegisterService {

    @Resource
    MallUserService mallUserService;

    /**
     * 用户注册
     * @param register 注册对象
     * @return 结果
     */
    @Override
    public String register(MallRegisterBody register) {
        String msg = "";
        String email = register.getEmail();
        String password = register.getPassword();
        if (StringUtils.isEmpty(email)) {
            msg = "邮箱不能为空";
        } else if (StringUtils.isEmpty(password)) {
            msg = "用户密码不能为空";
        } else if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
            msg = "密码长度必须在8到20个字符之间";
        }
        else if (mallUserService.checkEmailUnique(email)) {
            msg = "账号'" + email + "'注册失败，账号已存在";
        }
        else
        {
            MallUser mallUser = new MallUser();
            mallUser.setEmail(email);
            mallUser.setPassword(SecurityUtils.encryptPassword(password));
            boolean regFlag = mallUserService.registerUser(mallUser);
            if (!regFlag)
            {
                msg = "注册失败,请重新尝试";
            }
        }
        return msg;
    }
}
